package ups.glre.news.struts.action;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import ups.glre.news.model.News;
import ups.glre.news.model.User;
import ups.glre.news.service.CategoryService;
import ups.glre.news.service.NewsService;
import ups.glre.news.struts.form.NewsForm;

public class NewsAction extends DispatchAction{		

	private NewsService newsService = null;
	private CategoryService categoryService = null;
	
	public NewsAction() {
		InitialContext ic = null;
		try {
			ic = new InitialContext();
			newsService = (NewsService) ic.lookup("glre-news/NewsServiceImpl/local");
			categoryService = (CategoryService) ic.lookup("glre-news/CategoryServiceImpl/local");
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) 
			throws IOException, ServletException {
		
		NewsForm newsForm =(NewsForm)form;
		
		newsForm.setCategories(categoryService.getAllCategory());
		newsForm.setNewsList(newsService.getAllNews() );
		
		String forwardTo="succes";
		return mapping.findForward(forwardTo); 
	
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) 
			throws IOException, ServletException {		
		
		
		// ActionForward resultat = null;
		
		int userId = ((User) request.getSession().getAttribute("user")).getUserId();
		int categoryId = ((NewsForm) form).getCategoryId();
		String title = ((NewsForm) form).getTitle();
		String content = ((NewsForm) form).getContent();
		
		// Creates news service
		News news = newsService.createNews(userId, categoryId, title, content);
		
		//News news = newsService.createNews(1, 1, "title", "content");
		NewsForm newsForm =(NewsForm)form;
		newsForm.addNews(news);
		
		String forwardTo="succes";
		return mapping.findForward(forwardTo); 	
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) 
			throws IOException, ServletException { 
		NewsForm newsForm =(NewsForm)form;
		
		int newsId = ((NewsForm) form).getNewsId();
		
		News news = newsService.getGetNews(newsId);
		newsService.deleteNews(news);
		newsForm.removeNews(news);
		
		String forwardTo="succes";
		return mapping.findForward(forwardTo); 		
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) 
			throws IOException, ServletException { 
		
		NewsForm newsForm =(NewsForm)form;		
		int newsId = newsForm.getNewsId();
		News news = newsService.getGetNews(newsId);
		
		newsForm.setCategoryId(news.getId());
		newsForm.setTitle(news.getTitle());
		newsForm.setContent(news.getContent());
		
		String forwardTo="succes";
		return mapping.findForward(forwardTo); 		
	}

}
